﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PersonalWebSite.Common
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited=true, AllowMultiple=false)]
    public sealed class CacheControlAttribute : ActionFilterAttribute
    {
        #region Variables
        HttpCacheability cacheability;
        #endregion

        public CacheControlAttribute(HttpCacheability cacheability)
        {
            this.cacheability = cacheability;
        }

        /// <summary>
        /// Gets the HTTP Cacheability
        /// </summary>
        public HttpCacheability Cacheability { get { return cacheability; } }

        /// <summary>
        /// Handles the Action executed logic
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
            cache.SetCacheability(cacheability);
            base.OnActionExecuted(filterContext);
        }
    }
}